class Report < ActiveRecord::Base
  CHECK = {'未送审' => 0,'已送审' => 1,'已采用' => 2,'未采用' => 3}

  paginates_per 20

  validates_presence_of :title,:place,:reporter,:time,:department

  #default_scope ->{ self.order("created_at desc") }
  scope :check,->(check){check.blank? ? nil : where(:check => check.to_i)}
  scope :submited_reports,->{where(:check => [1,2,3])}
  scope :on_index,->(on_index){on_index.blank? ? nil : where(:on_index => on_index.to_i)}
  scope :report_id,->(report_id){report_id.blank? ? nil : where(:id => report_id.to_i)}
  scope :pubtime,->(begin_date,end_date){(begin_date.blank? && end_date.blank?) ? nil : where(:created_at => begin_date.to_date.beginning_of_day..end_date.to_date.at_end_of_day) }
  scope :department,->(department){department.blank? ? nil : where(:department => department)}
  
  def check_text
    #CHECK.map{|k,v| [v,k]}.to_h[check]
    case check
      when 0
        '未送审'
      when 1
        '已送审'
      when 2
        '已采用'
      when 3
        '未采用'
    end
  end

  #稿件类型（我的稿件，平台稿件）
  def self.report_type type,user
    case type
      when "1"
        self.where(:postby => user.name)
      when "2"
        if user.admin?
          self.submited_reports
        else
          Report.where("1=2")
        end
    end
  end

  def self.can_edit? report,user
    if report.check == 0 || user.admin?
      true
    else
      false
    end
  end

  def self.can_approve? report,user
    if report.check == 1 && user.admin?
      true
    else
      false
    end
  end
end
